/*
 * @Author: chenbolin@msn.cn
 * @Date: 2023-08-04 14:14:50
 * @LastEditors: chenbolin@msn.cn
 * @LastEditTime: 2023-10-27 17:21:24
 * @FilePath: \Remote\src\Sys\Sys.h
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
#ifndef __SYS_H
#define __SYS_H

#include "Hal.h"

#define SysTime_t uint32_t
#define SysTime HalGetTicks
#define SysTimeHasPast(old, past) (SysTime() - (old) > (past))

#define sysPrint printf
#define sysLog(...)                       \
    do                                    \
    {                                     \
        sysPrint("[%s]: ", __FUNCTION__); \
        sysPrint(__VA_ARGS__);            \
        sysPrint("\r\n");                 \
    } while (0)

#define SYS_PERIODIC_EXECUTION_INTERVAL 83 // 12 times in 1s
#define SYS_WIRELESS_BRAODCAST_ADDRESS 0xffffffff
#define SYS_VALVE_FREQUENCY 100

typedef enum
{
    SYS_WORK_MODE_NORMAL = 0, // 正常模式
    SYS_WORK_MODE_CONFIG      // 设置模式
} SysWorkMode_t;

typedef enum
{
    SYS_DEVICE_TYPE_GKZYC = 0, // 高空作业车
    SYS_DEVICE_TYPE_DGNSCD,    // 多功能随车吊
    SYS_DEVICE_TYPE_ZBD        // 直臂吊
} SysDeviceType_t;

typedef enum
{
    SYS_SPEED_MODE_SLOW = 0,
    SYS_SPEED_MODE_FAST
} SysSpeedMode_t;

/*系统故障代码
 * 系统故障后，将无法继续执行动作
 */
typedef enum
{
    SYS_ERROR_CODE_NONE = 0,              // 无
    SYS_ERROR_CODE_EMERGENCY_STOP_REMOTE, // 遥控器急停
    SYS_ERROR_CODE_WIERLESS_DISCONNECTED  // 无线断开
} SysErrorCode_t;

/*摇杆*/
typedef enum
{
    SYS_JOYSTICK_CH_DBSJ = 0, // 大臂升降
    SYS_JOYSTICK_CH_DBSS,     // 大臂伸缩
    SYS_JOYSTICK_CH_ZTXZ,     // 转台旋转
    SYS_JOYSTICK_CH_DGSJ,     // 吊钩升降
    SYS_JOYSTICK_CH_COUNT
} SysJoystickChannel_t;

typedef enum
{
    SYS_JOYSTICK_DIR_UP = 0,
    SYS_JOYSTICK_DIR_DOWN
} SysJoystickDirect_t;

/*比例阀*/
typedef enum
{
    SYS_VALVE_CH_DBSJ = 0, // 大臂升降
    SYS_VALVE_CH_DBSS,     // 大臂伸缩
    SYS_VALVE_CH_ZTXZ,     // 转台旋转
    SYS_VALVE_CH_DGSJ,     // 吊钩升降
    SYS_VALVE_CH_COUNT
} SysValveChannel_t;

typedef struct
{
    float raise;   // 升
    float descent; // 降
} SysValveStepValue_t;

typedef struct
{
    SysValveStepValue_t fast; // 快速
    SysValveStepValue_t slow; // 慢速
} SysValveStep_t;

// #pragma pack(1)
/*比例阀参数*/
typedef struct
{
    uint16_t min;
    uint16_t slowMax;
    uint16_t fastMax;
} SysValveCurrent_t;

typedef struct
{
    uint16_t flag;
    uint8_t version;
    uint32_t wirelessAddress;
    SysValveCurrent_t current[SYS_VALVE_CH_COUNT][2];
    uint8_t sumcheck;
} SysSettings_t;

// #pragma pack()
uint16_t SysThrottleValue(void);
SysSpeedMode_t SysSpeedMode(void);

void SysWirelessAddressSet(uint32_t addr);
uint32_t SysWirelessAddress(void);
uint8_t SysWirelessSignalLevel(void);

uint8_t SysBatteryPercentage(void);
uint16_t SysJoystickPercentToCurrent(SysValveChannel_t ch, SysJoystickDirect_t dir);
SysValveStep_t *SysGetValveStep(SysValveChannel_t ch, SysJoystickDirect_t dir);
void SysValveCurrentStepUpdate(SysValveChannel_t ch);
bool SysIsEmergencyStop(void);
SysSettings_t *SysSettings(void);
void SysSettingsUpdate(void);
char *SysVersion(void);
SysDeviceType_t SysDeviceType(void);
SysWorkMode_t SysWorkMode(void);

void SysInitialize(void);
void SysPoll(void);

#endif
